using System;
using Be.Timvw.Framework.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Be.Timvw.Framework
{
    [TestClass]
    public class WhenGettingCurrentDateTimeFromApplicationEnvironment
    {
        [TestMethod]
        public void ShouldUseSystemTimeWhenNoValuesAreProvided()
        {
            using(new ConfigurationFileSession("WithoutDateTimeManipulation.config"))
            {
                ApplicationEnvironment applicationEnvironment = new ApplicationEnvironment();

                DateTime configurationNow = DateTime.UtcNow;
                TimeSpan allowedDifference = TimeSpan.FromSeconds(1);
                Assert.IsTrue((applicationEnvironment.CurrentDateTime - configurationNow) < allowedDifference);
            }
        }

        [TestMethod]
        public void ShouldUseValuesAsProvidedInConfigurationFile()
        {
            using(new ConfigurationFileSession("WithDateTimeManipulation.config"))
            {
                ApplicationEnvironment applicationEnvironment = new ApplicationEnvironment();

                DateTime configurationNow = new DateTime(2000, 1, 1);
                TimeSpan allowedDifference = TimeSpan.FromSeconds(1);
                Assert.IsTrue((applicationEnvironment.CurrentDateTime - configurationNow) < allowedDifference);
            }
        }

        [TestMethod]
        public void ShouldUseValuesAsProvidedInConstructor()
        {
            DateTime instanceEpoch = DateTime.UtcNow;
            DateTime applicationEpoch = instanceEpoch;

            ApplicationEnvironment applicationEnvironment = new ApplicationEnvironment(instanceEpoch, applicationEpoch);
            DateTime configurationNow = instanceEpoch;
            TimeSpan allowedDifference = TimeSpan.FromSeconds(1);
            Assert.IsTrue((applicationEnvironment.CurrentDateTime - configurationNow) < allowedDifference);
        }
    }
}